using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;
using System.Net;
using System.Threading;

namespace TemperatureMicro
{
    public static class EthernetConfiguration                      // this class is greatly inspired by code found at this website http://www.mindthecloud.net/?p=619
    {                                                              // a few changes have been made to make it work though.
        public static void InitEthernetConfiguration()
        {
            var netInterface = NetworkInterface.GetAllNetworkInterfaces()[0];
            
            
            try
            {
                if (!netInterface.IsDynamicDnsEnabled)
                {
                    Debug.Print("Enabling Dynamic DNS");
                    netInterface.EnableDynamicDns();
                }

                if (!netInterface.IsDhcpEnabled)
                {
                    Debug.Print("Enabling Dhcp");
                    netInterface.EnableDhcp();
                    Thread.Sleep(1000);
                    netInterface.RenewDhcpLease();
                }

                Debug.Print("IP: " + netInterface.IPAddress.ToString());

                if (netInterface.IPAddress == IPAddress.Any.ToString())
                {
                    netInterface.RenewDhcpLease();
                }
                Thread.Sleep(2000);
            }
            catch
            {
                Debug.Print("DHCP Failed");
            }

            Debug.Print("Network Configuration");
            Debug.Print("IP " + netInterface.IPAddress.ToString());
            Debug.Print("Gateway " + netInterface.GatewayAddress.ToString());
            Debug.Print("DNS " + netInterface.DnsAddresses[0].ToString());
        }

    }
}
